package com.ctgu.demo.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ctgu.demo.dao.AdminMapper;
import com.ctgu.demo.entity.Admin;
import com.ctgu.demo.error.EmBusinessError;
import com.ctgu.demo.error.exception.BusinessException;
import com.ctgu.demo.service.AdminService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
 * @program:
 * @description:
 * @author: gh
 * @create: 2020-06-22 12:25
 **/
@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper adminMapper;

    @Override
    public Admin validateLogin(String telephone, String adminPassword) throws BusinessException {
        //通过手机号获取用户信息
        QueryWrapper<Admin> wrapper = new QueryWrapper<Admin>();
        wrapper.eq("administrator_telephone", telephone);
        Admin admin = adminMapper.selectOne(wrapper);
        if (admin == null) {
            throw new BusinessException(EmBusinessError.ADMIN_NOT_EXIST);
        }
        //比对用户加密码
        if (!StringUtils.equals(adminPassword, admin.getAdminPassword())) {
            throw new BusinessException(EmBusinessError.ADMIN_LOGIN_FAIL);
        }
        return admin;
    }
}
